<template>
<div class="main_box" style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
      <el-row>

        <el-form-item label="设备编号" prop="equipCode">
          <el-input v-model="dataForm.equipCode" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="equipName">
          <el-input v-model="dataForm.equipName"></el-input>
        </el-form-item>
        <el-form-item label="登陆账号" prop="adminUser">
          <el-input v-model="dataForm.adminUser" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="登陆密码" prop="adminPwd">
          <el-input v-model="dataForm.adminPwd" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="电量最低警报值" prop="powerMinWarn">
          <el-input v-model="dataForm.powerMinWarn"></el-input>
        </el-form-item>
        <el-form-item label="温度最低警报值" prop="temperatureMinWarn">
          <el-input v-model="dataForm.temperatureMinWarn"></el-input>
        </el-form-item>
        <el-form-item label="温度最高警报值" prop="temperatureMaxWarn">
          <el-input v-model="dataForm.temperatureMaxWarn"></el-input>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import Region from "@/components/Region";
import EquipApi from "@/api/equip/equip";
export default{
  data(){
      return {
        dataForm:{
          etId: "",
          equipName: "",
          equipCode: "",
          adminUser: "",
          adminPwd: "",
          powerMinWarn: "",
          temperatureMinWarn: "",
          temperatureMaxWarn: ""
        },
        loadingIns:false,
        //1.合作医院 2.其他医院
        hospitalTypeList: [
          { label: "合作医院", value: 1},
          { label: "其他医院", value: 2 }
        ],
        roleRules: {
          /*equipName: [
            { required: true, message: "请输入设备名称", trigger: "blur" },
            { max: 30, message: '长度不超过30个字', trigger: 'blur' }
          ],*/
          equipCode: [
            { required: true, message: "请输入设备编号", trigger: "blur" },
            { max: 20, message: '长度不超过20个字', trigger: 'blur' }
          ],
          adminUser: [
            { required: true, message: "请输入登录账号", trigger: "blur" }
          ],
          adminPwd: [
            { required: true, message: "请输入登录密码", trigger: "blur" }
          ]
        }
      }
  },
  components: {
    Region
  },
  methods: {
    init(){
      if (this.$route.query.id) {
        this.dataForm.etId = this.$route.query.id
        this.getData()
      }
    },
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.etId){
                this.editData();
              }else{
                this.addData();
              }
          });
        }
      })
    },
    addData() {
      EquipApi.addEquipTemperature(this.dataForm)
      .then(data => {
        this.$message({
          type: 'success',
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      })
    },
    editData() {
      EquipApi.editEquipTemperature(this.dataForm)
        .then(data => {
          this.$message({
            type: 'success',
            message: message.MSG_ALTER_SUCCESS
          });
          this.returnList();
        })
    },
    getData () {
      EquipApi.getEquipTemperatureInfo({
        etId: this.$route.query.id
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/equip/temperatureManagement'});
    }
  },
  mounted(){
    this.init()
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
